package com.fau.wmbj.server.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.fau.wmbj.server.entity.Menu;
import com.fau.wmbj.server.entity.Role;
import com.fau.wmbj.server.service.MenuService;
import com.fau.wmbj.server.service.RoleService;
import com.fau.wmbj.server.service.base.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 角色
 *
 * @author jiawei
 * @version 1.0
 * @datetime 2024-02-27 10:04
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role, Long> implements RoleService {

    @Resource
    private MenuService menuService;

    @Override
    public void update(Role role, List<Long> ids) {
        Long id = role.getId();
        Role before = super.find(id);
        Set<Menu> menuSet = new HashSet<>();
        if (CollectionUtil.isNotEmpty(ids)) {
            List<Menu> menuList = menuService.findList(ids);
            menuSet.addAll(menuList);
        }
        before.setMenus(menuSet);
        before.setCode(role.getCode());
        before.setName(role.getName());
        before.setDesc(role.getDesc());
        super.update(before);
    }

    @Override
    public void save(Role role, List<Long> ids) {
        Set<Menu> menuSet = new HashSet<>();
        if (CollectionUtil.isNotEmpty(ids)) {
            List<Menu> menuList = menuService.findList(ids);
            menuSet.addAll(menuList);
        }
        role.setMenus(menuSet);
        super.save(role);
    }
}
